package com.system.healthproject.service.imp;

import com.system.healthproject.common.ApiResult;
import com.system.healthproject.mapper.LoginMapper;
import com.system.healthproject.mapper.MenuMapper;
import com.system.healthproject.mapper.RoleMapper;
import com.system.healthproject.model.admin.paramsbean.MenuParams;
import com.system.healthproject.model.admin.paramsbean.RoleAddMenusParams;
import com.system.healthproject.model.admin.paramsbean.RoleParams;
import com.system.healthproject.pojo.Menu;
import com.system.healthproject.pojo.Role;
import com.system.healthproject.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleServiceImp implements RoleService {

    @Autowired
    RoleMapper roleMapper;
    @Autowired
    MenuMapper menuMapper;
    @Autowired
    LoginMapper loginMapper;

    /**
     * 给角色添加所有菜单
     * @param roleId
     * @return
     */
    @Override
    public Boolean roleAddAllMenu(Integer roleId) {
        List<Integer> integers = menuMapper.selectAllMenuId();
        int i = roleMapper.insertRoleAllMenu(roleId, integers);
        return i==1;
    }

    /**
     * 给角色添加菜单
     * @param roleId
     * @param menuId
     * @return
     */
    @Override
    public Boolean roleAddMenu(Integer roleId, Integer menuId) {
        return roleMapper.roleAddMenu(roleId,menuId)==1;
    }

    /**
     * 给用户添加角色
     * @param userId
     * @param roleId
     * @return
     */
    @Override
    public Boolean addUserRole(Integer userId, Integer roleId) {
        return loginMapper.addUserRole(userId,roleId)==1;
    }

    /**
     * 角色批量添加菜单
     * @param role
     * @return
     */
    @Override
    public Boolean roleAddMenus(RoleAddMenusParams role) {
        List<Integer> menuIds = role.getMenuIds();
        int i = roleMapper.insertRoleAllMenu(role.getRoleId(), menuIds);
        return  i>0;
    }

    @Override
    public Boolean roleDeleteMenus(RoleAddMenusParams role) {
        List<Integer> menuIds = role.getMenuIds();
        int i = roleMapper.deleteRoleAllMenu(role.getRoleId(), menuIds);
        return  i>0;
    }

    /**
     * 获取角色列表
     * @return
     */
    @Override
    public List<Role> getRoleList() {
        List<Role> roles = roleMapper.selectAllRole();
        return roles;
    }

    /**
     * 获取全部菜单
     * @return
     */
    @Override
    public List<Menu> getAllMenu() {
        List<Menu> menus = menuMapper.selectAllMenu();
        return menus;
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @Override
    public Boolean addRole(RoleParams role) {
        Role record = new Role();
        record.setRoleName(role.getRoleName());
        record.setRoleNamezh(role.getRoleNamezh());
        record.setRoleParentid(role.getParentId());
        int insert = roleMapper.insert(record);
        return insert==1;
    }

    /**
     * 添加菜单
     * @param menuParams
     * @return
     */
    @Override
    public Boolean addMenu(MenuParams menuParams) {
        Menu menu = new Menu();
        menu.setMenuName(menuParams.getMenuName());
        menu.setMenuNamezh(menuParams.getMenuNamezh());
        menu.setParentId(menuParams.getParentId());
        menu.setUrl(menuParams.getUrl());
        int insert = menuMapper.insert(menu);
        return insert==1;
    }

    /**
     * 用户修改角色
     * @param userId
     * @param roleId
     * @return
     */
    @Override
    public Boolean userChangeRole(Integer userId, Integer roleId) {
        int i = roleMapper.userChangeRole(userId, roleId);
        return i==1;
    }

    /**
     * 修改菜单
     * @param menu
     * @return
     */
    @Override
    public Boolean changeMenu(Menu menu) {
        int i = menuMapper.updateByPrimaryKeySelective(menu);
        return i==1;
    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    @Override
    public Boolean changRole(Role role) {
        int i = roleMapper.updateByPrimaryKeySelective(role);
        return i==1;
    }

    /**
     * 根据角色查询菜单
     * @param roleId
     * @return
     */
    @Override
    public List<Menu> getMenuByRole(Integer roleId) {
        List<Menu> menus = menuMapper.selectMenuByRole(roleId);
        return menus;
    }

    /**
     * 根据昵称模糊查询
     * @param name
     * @return
     */
    @Override
    public List<Menu> selectMenuByName(Menu name) {
        List<Menu> menus = menuMapper.selectMenuByName(name);
        return menus;
    }

    /**
     * 删除菜单
     * @param menuId
     * @return
     */
    @Transactional
    @Override
    public boolean deleteMenu(Integer menuId) {
        menuMapper.deleteRoleMenuByMenuId(menuId);
        int i = menuMapper.deleteByPrimaryKey(menuId);
        return i==1;
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @Override
    public ApiResult<Object> deleteRoleById(Integer roleId) {
        Integer i = roleMapper.hasUserCount(roleId);
        if(i!=null&&i>0){
            return ApiResult.failure("删除失败,当前角色下有用户");
        }
        int i1 = roleMapper.deleteByPrimaryKey(roleId);

        return ApiResult.withBoolean(i1==1,"删除角色成功","删除角色失败");
    }


}
